package org.jeecg.common.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

/**
 * 订单编号策略
 *
 * @author light-zhang
 * @version 1.0.0
 * @project common-utils
 * @fileName ODDGenerator.java
 * @Description
 * @date 2018年5月11日
 */
public abstract class ODDGenerator {
    private static final FastDateFormat pattern = FastDateFormat.getInstance("yyyyMMddHHmmss");
    private static final AtomicInteger atomicInteger = new AtomicInteger(1);
    private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<StringBuilder>();

    /**
     * 【长码生成策略】
     *
     * @param lock 生成的UUID32位参数
     * @return 长码机制
     * @时间20180511231532
     * @二位随机数
     * @lock的hash-code编码
     */
    public static String getC(String lock) {
        StringBuilder builder = new StringBuilder(pattern.format(Instant.now().toEpochMilli()));// 取系统当前时间作为订单号前半部分
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        threadLocal.set(builder);
        return threadLocal.get().toString();
    }

    /**
     * 【短码生成策略】
     *
     * @param lock
     * @return
     */
    public static synchronized String getD(String lock) {
        StringBuilder builder = new StringBuilder(ThreadLocalRandom.current().nextInt(0, 999));// 随机数
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        return builder.toString();
    }


    public static List<String> getSn(String productCode, int lot, int num, LocalDate date) {
        List<String> result = new ArrayList<String>(num);
        String dateStr = date.format(DateTimeFormatter.ofPattern("yyMMdd"));
        String sub = dateStr.substring(2, 4);
        int month = Integer.parseInt(sub);
        if (month > 9) {
            StringBuilder b = new StringBuilder(dateStr);
            if (month == 10) {
                b.replace(2, 4, "A");
            }
            if (month == 11) {
                b.replace(2, 4, "B");
            }
            if (month == 12) {
                b.replace(2, 4, "C");
            }
            dateStr = b.toString();
        }
        StringBuilder builder = new StringBuilder(productCode).append(dateStr).append(String.format("%02d", lot));
        for (int i = 1; i <= num; i++) {
            String n = String.format("%04d", i);
            String sn = builder.toString();
            result.add(sn + n);
        }
        return result;
    }

    /**
     * 1000个线程并发测试
     *
     * @param args
     * @throws InterruptedException
     * @throws ExecutionException
     */
    public static void main(String[] args) throws InterruptedException, ExecutionException {
      /*  List<String> p1 = getSn("P1", 1, 500,new Date());
        p1.forEach(
                e->
                System.out.print(e + "\n")
        );*/
    }
}
